<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace api\controllers;

use Yii;
use yiike\user\models\Token;
use common\helpers\CommonHelper;
use common\models\LoginForm;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * 绑定Qq 微信
 *
 * @author Administrator
 */
class ConnectController extends \api\components\Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'qq'     => ['post'],
                    'weixin' => ['post'],
                ],
            ],
        ];
    }

    public function actionQq() {
        echo Yii::$app->user->id;die;
        $openid       = Yii::$app->request->post('openid');
        $access_token = Yii::$app->request->post('access_token');
        
        return ['ss'];
    }

    public function actionWeixin($login, $password) {
        $openid       = Yii::$app->request->post('openid');
        $access_token = Yii::$app->request->post('access_token');

        $loginForm = Yii::createObject(LoginForm::className());
        $loginForm->setAttributes([
            'login'    => $login,
            'password' => $password
        ]);

        if ($loginForm->login()) {

            $user    = Yii::$app->user->identity;
            $profile = $user->profile;
            $token   = Yii::createObject(['class' => Token::className(), 'type' => Token::TYPE_LOGIN]);
            $token->link('user', $user);

            return ['code' => 0, 'message' => '登录成功', 'username' => $user->username, 'name' => $profile->name, 'avatar' => $profile->avatar, 'token' => $token->code];
        }
        else {
            return ['code' => 100029, 'message' => $this->arrayToString($loginForm->errors)];
        }
    }
}
